home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / dev / src / rot3dsrc.lha / rot3d / makewall.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-11  |  5.6 KB  |  245 lines

  1. /* this program takes a list of IFF files and generates wall data from them
  2. Iffs must be 288X75X4 and must be of one of the 2 pallettes that you choose.
  3. Walls are converted to our own semi-chunky mode so that the megadraw routine
  4. can digest them easier.  500 converter would want to modify this to create
  5. 2 2bitplane chunks.  This program outputs the file walls.c which is then made
  6. into the main program. */
  7.  
  8. #define DEPTH 4
  9. #define WIDTH 256
  10. #define HEIGHT 128
  11.  
  12. void BreakWall(struct BitMap *brush);
  13. void PrintWall(struct BitMap *brush,char *name,short num);
  14. void FreePlanes(struct BitMap *brush);
  15.  
  16. struct BitMap *brush=NULL;
  17. struct ILBM_info *info;
  18. FILE *bfile,*cfile,*hfile,*script;
  19. char *c;
  20. char scanval[100];
  21. BYTE palnums[500];
  22. char Name[100];
  23. char Palette[10];
  24. BYTE palno;
  25.  
  26. main(int argc,char **argv)
  27. {
  28.     short i,brushno,numbrushes;
  29.  
  30.     numbrushes=argc-1;
  31.     if (numbrushes==0) {
  32.         printf("Usage: %s <script name>\n",argv[0]);
  33.         exit(1);
  34.     }
  35.  
  36.     cfile=fopen("walls.c","w");
  37.     if (!cfile)    {
  38.         printf("Could not open walls.c for write\n");
  39.         FreePlanes(brush);
  40.         exit(1);
  41.     }
  42.  
  43.     bfile=fopen("walls.dat","w");
  44.     if (!bfile)    {
  45.         printf("Could not open walls.dat for write\n");
  46.         FreePlanes(brush);
  47.         fclose(cfile);
  48.         exit(1);
  49.     }
  50.  
  51.     hfile=fopen("walls.h","w");
  52.     if (!hfile)    {
  53.         printf("Could not open walls.h for write\n");
  54.         FreePlanes(brush);
  55.         fclose(cfile);
  56.         fclose(bfile);
  57.         exit(1);
  58.     }
  59.  
  60.     script=fopen(argv[1],"r");
  61.     if (!script) {
  62.         printf("Could not open script file %s\n",argv[1]);
  63.         fclose(hfile);
  64.         fclose(cfile);
  65.         fclose(bfile);
  66.         goto QUIT;
  67.     }
  68.  
  69.     numbrushes=0;
  70.  
  71.     fprintf(cfile,"#include \"walls.h\"\n");
  72.     while(c=fgets(scanval,98,script)) {
  73.         numbrushes++;
  74.         sscanf(scanval,"%s %s",Name,Palette);
  75.         palno=atoi(Palette);
  76.         printf("%s %d\n",Name,palno);
  77.         info=read_iff(Name);
  78.         if (!info) {
  79.             printf("Could not open %s for read\n",Name);
  80.             fclose(hfile);
  81.             goto QUIT;
  82.         }
  83.         brush=&info->bitmap;
  84.         if(numbrushes==1) {
  85.             if (brush->Depth!=5) {
  86.                 printf("Aborting on %s\n",Name);
  87.                 printf("brush depth %d != 5\n",brush->Depth);
  88.                 FreePlanes(brush);
  89.                 goto QUIT;
  90.             }
  91.             fprintf(cfile,"unsigned short palette[] = {");
  92.             for    (i=0; i<(1<<5)*3; i+=3)
  93.             {
  94.                 if (!(i%4))    fprintf(cfile,"\n");
  95.                 fprintf(cfile,"0x%.4X, ",(info->cmap[i]/16)<<8|
  96.                 (info->cmap[i+1]/16)<<4|info->cmap[i+2]/16);
  97.             }
  98.             fprintf(cfile,"\n");
  99.             fprintf(cfile,"};\n");
  100.             FreePlanes(brush);
  101.         } else {
  102.             palnums[numbrushes-2]=palno;
  103.             if (brush->Depth!=4) {
  104.                 printf("Aborting on %s\n",Name);
  105.                 printf("brush depth %d != DEPTH\n",brush->Depth);
  106.                 FreePlanes(brush);
  107.                 goto QUIT;
  108.             }
  109.             if (brush->BytesPerRow!=WIDTH/8) {
  110.                 printf("Aborting on %s\n",Name);
  111.                 printf("brush width %d != WIDTH\n",brush->BytesPerRow*8);
  112.                 FreePlanes(brush);
  113.                 goto QUIT;
  114.             }
  115.             if (brush->Rows!=HEIGHT) {
  116.                 printf("Aborting on %s\n",Name);
  117.                 printf("brush rows %d != HEIGHT\n",brush->Rows);
  118.                 FreePlanes(brush);
  119.                 goto QUIT;
  120.             }
  121.  
  122.             BreakWall(brush);
  123.  
  124.             PrintWall(brush,Name,numbrushes-2);
  125.  
  126.             FreeMem(brush->Planes[0],brush->BytesPerRow*brush->Rows*4);
  127.         }
  128.     }
  129.  
  130.     fprintf(hfile,"#define NUMWALLS %d\n",numbrushes-1);
  131.     fprintf(hfile,"extern unsigned long *brushmem[NUMWALLS];\n");
  132.     fprintf(hfile,"extern unsigned short palette[];\n");
  133.     fprintf(hfile,"extern char brushpal[];\n");
  134.     fclose(hfile);
  135.  
  136.     fprintf(cfile,"unsigned long *brushmem[NUMWALLS];\n");
  137.     fprintf(cfile,"char brushpal[%d]= {",numbrushes-1);
  138.     for    (brushno=0;    brushno<numbrushes-1; brushno++) {
  139.         if(!(brushno%8)) fprintf(cfile,"\n");
  140.         fprintf(cfile," %d,",palnums[brushno]);
  141.     }
  142.     fprintf(cfile,"\n};\n");
  143.     fprintf(cfile,"void loadwalls() {\n");
  144.     fprintf(cfile,"\tFILE *file; int i;\n");
  145.     fprintf(cfile,"\tchar *mem;\n");
  146.     fprintf(cfile,"\n");
  147.     fprintf(cfile,"\tmem=(char *)malloc(NUMWALLS*32768);\n");
  148.     fprintf(cfile,"\tif(!mem) exit(1);\n");
  149.     fprintf(cfile,"\tfile=fopen(\"walls.dat\",\"r\");\n");
  150.     fprintf(cfile,"\tfread(mem,32768,NUMWALLS,file);\n");
  151.     fprintf(cfile,"\tfor(i=0;i<NUMWALLS;i++)\n");
  152.     fprintf(cfile,"\t\tbrushmem[i]=(unsigned long *)(mem+i*32768);\n");
  153.     fprintf(cfile,"}\n");
  154.  
  155.  
  156. QUIT:
  157.  
  158.     fclose(script);
  159.     fclose(cfile);
  160.     fclose(bfile);
  161.  
  162.     exit(0);
  163. }
  164.  
  165. void PrintWall(struct BitMap *brush,char *name,short num)
  166. {
  167.     short i,j,k;
  168.     ULONG val;
  169.  
  170.     for (j=0;j<brush->Rows*2;j++) {
  171.         for (k=0;k<brush->BytesPerRow;k++) {
  172.             val=*(ULONG *)(brush->Planes[0]+k*4+j*brush->BytesPerRow*4);
  173.             fwrite((void *)&val,4,1,bfile);
  174.         }
  175.     }
  176. }
  177.  
  178. void FreePlanes(struct BitMap *brush)
  179. {
  180.     short i;
  181.     for (i=0;i<4;i++)
  182.     {
  183.         FreeMem(brush->Planes[i],brush->BytesPerRow*brush->Rows);
  184.         brush->Planes[i]=0;
  185.     }
  186. }
  187.  
  188.  
  189. void BreakWall(struct BitMap *brush)
  190. {
  191. short i,j,k,l,bit;
  192. UBYTE current=0;
  193. PLANEPTR new;
  194.     new = AllocMem(brush->BytesPerRow*brush->Rows*4*2,MEMF_FAST);
  195.     if (!new) {
  196.         printf("Could not get fast ram for brush!\n");
  197.         FreePlanes(brush);
  198.         fclose(cfile);
  199.         exit(1);
  200.     }
  201.     for (j=0;j<brush->Rows;j++)
  202.         for (k=0;k<brush->BytesPerRow;k++) {
  203.             for(bit=7;bit>=0;bit--) {
  204.                 current=0;
  205.                 for    (i=0;i<4;i++) {
  206.                     UBYTE temp;
  207.                     temp=*(UBYTE *)(brush->Planes[i]+(j*brush->BytesPerRow+k))&(1<<bit);
  208.                     temp=temp>>bit;
  209.                     current|=temp<<((i)+2);
  210.                 }
  211.                 *(UBYTE    *)(new+(7-bit)+k*8+j*brush->BytesPerRow*8)=current;
  212.             }
  213.         }
  214.     FreePlanes(brush);
  215.     brush->Planes[0]=new;
  216. }
  217.  
  218.  
  219. void OldBreakWall(struct BitMap *brush)
  220. {
  221. short i,j,k,l;
  222. UBYTE current=0;
  223. PLANEPTR new;
  224.  
  225.     new = AllocMem(brush->BytesPerRow*brush->Rows*4,MEMF_FAST);
  226.     if (!new) {
  227.         printf("Could not get fast ram for brush!\n");
  228.         FreePlanes(brush);
  229.         fclose(cfile);
  230.         exit(1);
  231.     }
  232.  
  233.     for (i=0;i<4;i++)
  234.     {
  235.         for (j=0;j<brush->Rows;j++)
  236.             for (k=0;k<brush->BytesPerRow;k++)
  237.             {
  238.                 current=*(UBYTE *)(brush->Planes[i]+(j*brush->BytesPerRow+k));
  239.                 *(UBYTE *)(new+i+k*4+j*brush->BytesPerRow*4)=current;
  240.             }
  241.     }
  242.     FreePlanes(brush);
  243.     brush->Planes[0]=new;
  244. }
  245.